<template>
	<view>
		<navbar title="购物车" bg="#F8F3E7" titleColor="#400303" leftIcon='line' />

		<view class="sc-main">
			<view class="header  sticky flex align-item_center justify-content_space-between">
				<view class="info flex align-item_center">
					<view class="active">全部 {{validList.length}}</view>
					<!-- <view>降价 33</view> -->
				</view>
				<!-- <image class="v-seatch-img block flex-shrink0" src="https://mall-admin.xinjiafu.net/mini-static/images/points/4.png" /> -->
			</view>
			<view class="main border-box">
				<up-checkbox-group v-model="cardSelects" placement="column" @change="changeCard">
					<up-swipe-action>
						<up-swipe-action-item :options="swipeOptions" v-for="item in validList" :key="item.id"
							@click="delAddress(item)" class="item">
							<view class="item border-box">
								<view class="product flex align-item_center">
									<up-checkbox class="flex-shrink0" :name="item.id" shape="circle" activeColor="#A72530"
										inactiveColor="#EDE2CD">
									</up-checkbox>
									<image class="cover flex align-item_center justify-content_center" :src="item.spu.picUrl"
										mode="aspectFit">
									</image>
									<view class="info">
										<view class="name">{{item.spu.name}}</view>
										<view class="types border-box">{{item.sku.properties.map(pro=>pro.valueName).join(' ')}}</view>
										<view class="points flex align-item_center justify-content_space-between">
											<view class="num">
												{{fen2yuan(item.sku.price,0)}}<text>积分</text>
											</view>
											<view class="stepsNum">
												<up-number-box v-model="item.count" @change="(val) => onNumberChange(val,item)"></up-number-box>
											</view>
										</view>
									</view>
								</view>
							</view>
						</up-swipe-action-item>
					</up-swipe-action>
				</up-checkbox-group>
			</view>
		</view>
		<u-gap height="170rpx"> </u-gap>
		<view class="ms-foot fixed flex align-item_center justify-content_space-between">
			<view class="left flex align-item_center">
				<view class="select" @click="changeSelect">{{allSelect?"取消全选":'全选'}}</view>
				<view class="money">
					合计：
					<text>{{ fen2yuan(payCount,0)}}</text>
				</view>
			</view>
			<view class="count" @click="pay">去结算（{{cardSelects.length}}）</view>
		</view>
		<tabbar />
	</view>
</template>

<script setup>
	import {
		onMounted,
		getCurrentInstance,
		ref,
		reactive,
		computed
	} from "vue";
	import navbar from "@/components/navbar.vue"
	import tabbar from "@/components/tabbar.vue"
	import {
		navigateTo
	} from "@/utils/tool";
	import CartApi from "@/sheep/api/trade/cart.js"
	import {
		onShow
	} from "@dcloudio/uni-app"
	import {
		fen2yuan
	} from '@/sheep/hooks/useGoods';
	import sheep from '@/sheep';


	const swipeOptions = reactive([{
		text: '删除'
	}]);
	const delAddress = (val) => {
		CartApi.deleteCart(val.id).then(() => {
			cartList()
		})
	}

	const allSelect = computed(() => cardSelects.value.length >= validList.value.length)

	const changeSelect = () => {
		const ids = validList.value.map(item => item.id)

		const data = {
			ids,
			selected: !allSelect.value
		}

		updateCartSelected(data)
	}

	const cardSelects = ref([]);
	const cart = sheep.$store('cart');

	const changeCard = (val) => {
		const newLs = val.filter(item => !cardSelects.value.includes(item));
		const delLs = cardSelects.value.filter(item => !val.includes(item));

		const data = {
			ids: newLs.length > 0 ? newLs : delLs,
			selected: newLs.length > 0 ? true : false,
		}

		updateCartSelected(data)
	}

	const updateCartSelected = (data) => {
		CartApi.updateCartSelected(data).then(res => {
			cartList()
		})
	}

	function onNumberChange(val, item) {
		const data = {
			count: val.value,
			id: item.id
		}
		CartApi.updateCartCount(data).then(() => {
			cartList()
		})
	}

	const stepNumber = ref(0)

	const pay = () => {
		if (cardSelects.value.length === 0) return sheep.$helper.toast('请选择商品');
		cardSelects.value.map

		const valid = validList.value.filter(item => {
			if (cardSelects.value.includes(item.id)) return item;
		});

		const items = valid.map(item => {
			return {
				skuId: item.sku.id,
				count: item.count,
				cartId: item.id
			}
		})

		navigateTo(`/points/mall_pay/index?items=${JSON.stringify(items)}`)
	}

	const validList = ref([]);
	const payCount = ref(0);

	const cartList = () => {
		CartApi.getCartList().then(res => {
			validList.value = res.data.validList
			cardSelects.value = []
			payCount.value = 0
			res.data.validList.forEach(item => {
				if (item.selected) {
					payCount.value += item.count * item.sku.price
					cardSelects.value.push(item.id)
				}
			})
		})
	}
	onShow(() => {
		cartList()
	})
</script>


<style>
	page {
		background: #F8F3E7;
	}
</style>
<style scoped lang="scss">
	:deep(.u-swipe-action-item__content) {
		background-color: #EDE2CD !important;
	}

	.ms-foot {
		left: 0;
		bottom: 0;
		z-index: 10;
		width: 750rpx;
		height: 100rpx;
		border-top: 1rpx solid rgba(0, 0, 0, 0.08);
		background: #F8F3E9;
		padding-bottom: calc(0px + 50px);
		padding-bottom: calc(constant(safe-area-inset-bottom) + 50px);
		padding-bottom: calc(env(safe-area-inset-bottom) + 50px);

		.count {
			margin-right: 30rpx;
			width: 200rpx;
			height: 56rpx;
			line-height: 56rpx;
			text-align: center;
			border-radius: 100rpx;
			background: #A72530;
			font-size: 28rpx;
			font-weight: 400;
			color: rgba(255, 255, 255, 1);
		}

		.left {
			font-size: 36rpx;
			font-weight: 400;
			color: rgba(0, 0, 0, 1);
			margin-left: 30rpx;

			.select {
				width: 150rpx;
			}

			.money {
				margin-left: 42rpx;

				text {
					color: rgba(167, 37, 48, 1);
				}
			}
		}
	}

	.sc-main {
		padding: 30rpx 20rpx;

		.main {
			background-color: #EDE2CD;
			border-radius: 8rpx;
			width: 710rpx;
			padding: 34rpx 32rpx;

			:deep(.u-swipe-action-item:not(:first-child)) {
				margin: 56rpx auto 0 !important;
			}

			// .item:not(:first-child) {
			// 	margin: 66rpx auto 0 !important;
			// }

			.item {

				.product {

					.info {
						.points {
							margin-top: 14rpx;


							.count {
								font-size: 24rpx;
								font-weight: 400;
								letter-spacing: 0rpx;
								line-height: 34.76rpx;
								color: rgba(0, 0, 0, 1);
							}

							.num {
								font-size: 40rpx;
								font-weight: 400;
								letter-spacing: 0rpx;
								line-height: 57.92rpx;
								color: rgba(167, 37, 48, 1);

								text {
									font-size: 24rpx;
									font-weight: 400;
									letter-spacing: 0rpx;
									line-height: 57.92rpx;
									color: rgba(64, 3, 3, 1);
								}
							}
						}

						.types {
							margin-top: 16rpx;
							min-width: 322rpx;
							width: fit-content;
							height: 40rpx;
							line-height: 40rpx;
							padding: 0 28rpx;
							opacity: 1;
							border-radius: 100rpx;
							font-size: 24rpx;
							font-weight: 400;
							letter-spacing: 0rpx;
							color: rgba(166, 123, 123, 1);
							background: #F8F3E7;
						}

						.name {
							width: 416rpx;
							font-size: 28rpx;
							font-weight: 400;
							letter-spacing: 0rpx;
							line-height: 40.54rpx;
							color: rgba(64, 3, 3, 1);
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}
					}

					.cover {
						width: 160rpx;
						height: 160rpx;
						opacity: 1;
						border-radius: 8rpx;
						background: #FFFFFF;
						margin-right: 16rpx;
						margin-left: 4rpx;

						image {
							width: 128rpx;
							height: 128rpx;
						}
					}
				}

				.head {
					view:last-child {
						font-size: 24rpx;
						font-weight: 400;
						letter-spacing: 0rpx;
						line-height: 34.76rpx;
						color: rgba(167, 37, 48, 1);
						margin-right: 22rpx;
					}

					view:first-child {
						font-size: 24rpx;
						font-weight: 400;
						letter-spacing: 0rpx;
						line-height: 34.76rpx;
						color: rgba(102, 102, 102, 1);
					}
				}
			}
		}

		.header {
			top: var(--navbarH);
			z-index: 10;
			background-color: #F8F3E7;
			padding-bottom: 10rpx;

			.info {
				view:last-child {
					// margin-left: 64rpx
				}

				view {
					font-size: 28rpx;
					font-weight: 400;
					color: rgba(64, 3, 3, 1);
					transition: color .2s;
				}

				.active {
					color: rgba(167, 37, 48, 1);
				}
			}
		}
	}
</style>